package com.yuqiang.homePage.service.impl;

import com.yuqiang.homePage.model.vo.SessionVo;
import com.yuqiang.user.dao.KeyDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuqiang.common.model.ResultModel;
import com.yuqiang.common.util.BusinessConstant;
import com.yuqiang.homePage.dao.HomePageDao;
import com.yuqiang.homePage.model.bo.HomePageBo;
import com.yuqiang.homePage.model.po.HomePagePo;
import com.yuqiang.homePage.service.HomePageService;

import java.util.List;

@Service("homePageService")
public class HomePageServiceImpl implements HomePageService {

	@Autowired
	private HomePageDao homePageDao;
	
	private Logger logger = LoggerFactory.getLogger(HomePageServiceImpl.class);

	//检查key是否有效
	@Override
	public ResultModel checkKey(HomePageBo homePageBo) {
		ResultModel resultModel = new ResultModel();
		if(homePageDao.checkKey(homePageBo)==null||homePageDao.checkKey(homePageBo).size()<BusinessConstant.NUMBER_ONE){
			resultModel.setSuccess(false);
			resultModel.setMessage(BusinessConstant.KEY_IS_INVALID);
			return resultModel;
		}
		resultModel.setKeyVo(homePageDao.checkKey(homePageBo).get(0));
		return resultModel;
	}


	//检查用户名是否重复
	@Override
	public ResultModel checkUserName(HomePageBo homePageBo) {
		ResultModel resultModel = new ResultModel();
		if(homePageDao.checkUserName(homePageBo)>BusinessConstant.NUMBER_ZERO){
			resultModel.setSuccess(false);
			resultModel.setMessage(BusinessConstant.USERNAME_IS_EXIST);
		}
		return resultModel;
	}

	//登录验证账号密码
	@Override
	public ResultModel checkLogin(HomePageBo homePageBo) {
		ResultModel resultModel = new ResultModel();
		if(homePageDao.checkLogin(homePageBo)<BusinessConstant.NUMBER_ONE){
			resultModel.setSuccess(false);
			resultModel.setMessage(BusinessConstant.USERNAME_OR_PASSWORD_ERROR);
		}
		return resultModel;
	}

	//注册用户
	@Override
	public ResultModel registerUser(HomePageBo homePageBo) {
		ResultModel resultModel = new ResultModel();
		HomePagePo homePagePo = new HomePagePo();
		homePagePo.setUserName(homePageBo.getUserName());
		homePagePo.setUserIsvalid(BusinessConstant.STATUS_VALID_T);
		homePagePo.setUserType(homePageBo.getUserType());
		homePagePo.setUserPassword(homePageBo.getPassword());
		try{
			homePageDao.registerUser(homePagePo);
			return resultModel;
		}catch(Exception e){
			logger.error(BusinessConstant.SQL_EXCEPTION, e);
			resultModel.setSuccess(false);
			resultModel.setMessage(BusinessConstant.SQL_EXCEPTION_MESSAGE);
			return resultModel;
		}
	}

	@Override
	public List<SessionVo> getSession(String userName) {
		return homePageDao.getSession(userName);
	}

}
